#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/22  20:18
# @Author: 余浪人
# @email: yulangren520@gmail.com
import os,json
from wtforms import Form, StringField, validators, BooleanField, TextAreaField
from apps.models.synthesize_model import TL_ThemePush_Model


class TL_Connector_Form(Form):
    status = BooleanField('接口开关', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭"})
    impower_code = StringField('授权码', render_kw={"class": "layui-input",'onclick':"copyText('impower_code')",'readonly':'true'})


class TL_WebApp_Form(Form):
    status = BooleanField('接口开关', render_kw={"lay-skin": "switch", "lay-text": "开启|关闭"})
    public_key = StringField('授权 KEY', validators=[validators.DataRequired('请输入授权 KEY')], render_kw={"class": "layui-input"})
    private_code = StringField('授权码', render_kw={"class": "layui-input",'readonly':'true','onclick':"copyText('private_code')"})
    public_pem = TextAreaField('加密公钥', render_kw={"class": "layui-textarea",'readonly':'true','rows':'9','onclick':"copyText('public_pem')"})


class TL_Email_Form(Form):
    email_title = StringField('邮件标题', validators=[validators.DataRequired('请输入标题')], render_kw={"class": "layui-input"})
    email_addressee = StringField('收件人', validators=[validators.DataRequired('请输入标题'), validators.Email('请输入合法的收件邮箱')], render_kw={"class": "layui-input", "style": "width:30%"})
    email_content = TextAreaField('邮件内容', render_kw={"class": "layui-textarea", "rows": "17", "style": "width:80%"})


class TL_Indexes_Form(Form):
    indexes_design = StringField('索引结构',validators=[validators.DataRequired('请填写索引结构')],render_kw={"class": "layui-input","placeholder": "http://"})
    push_address = StringField('推送地址',validators=[validators.DataRequired('请填写推送地址'),validators.URL(message='推送地址不是一个合法URL')],default='http://data.zz.baidu.com/urls?site=xxxxxxx',render_kw={"class": "layui-input","placeholder": "前往百度资源搜索平台"})
    indexes_num = StringField('提交索引量',validators=[validators.DataRequired('请输入提交索引量')],default=400,render_kw={"class": "layui-input",'style':"width: 200px"})
    def validate_indexes_num(self, indexes_num):
        try:
            val = int(indexes_num.data)
        except ValueError:
            raise validators.ValidationError('索引数类型错误,只限输入整数')
        if val > 4000 or val < 1:
            raise validators.ValidationError('提交索引最大:4000 最小为:1')


class TL_ThemePush_Form(Form):
    identifier = StringField('模板标识')

    def validate_identifier(self, identifier):
        data = identifier.data
        theme_path = f'{os.getcwd()}/apps/templates/web_template'.replace('\\','/')
        if not data: raise validators.ValidationError('请输入主题模板标识')
        elif data not in os.listdir(theme_path):raise validators.ValidationError('系统未检索到指定主题')
        elif not os.path.isdir(f'{theme_path}/{data}/templates'):raise validators.ValidationError('系统未检测到templates目录')
        elif not len(os.listdir(f'{theme_path}/{data}/templates')):raise validators.ValidationError('系统未检测到模板文件')
        elif 'info.json' not in  os.listdir(f'{theme_path}/{data}'):raise validators.ValidationError('系统为检测到info.json 配置')
        try:
            with open(f'{theme_path}/{data}/info.json','r',encoding='utf-8')as fp: info = json.loads(fp.read())
            if info.get('identifier','')!= data:raise validators.ValidationError('主题模板标识不一致')
        except:
            raise validators.ValidationError('主题模板配置解析失败')
        theme = TL_ThemePush_Model.query.filter_by(identifier=data).first()
        if theme:raise validators.ValidationError('模板已安装,不可重复安装')